package com.ruoyi.ldzlsb.service.impl;

import java.util.List;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ldzlsb.mapper.SbInspectionConfigMapper;
import com.ruoyi.ldzlsb.domain.SbInspectionConfig;
import com.ruoyi.ldzlsb.service.ISbInspectionConfigService;

/**
 * 巡检配置Service业务层处理
 * 
 * @author anli
 * @date 2025-01-11
 */
@Service
public class SbInspectionConfigServiceImpl implements ISbInspectionConfigService 
{
    @Autowired
    private SbInspectionConfigMapper sbInspectionConfigMapper;

    /**
     * 查询巡检配置
     * 
     * @param configId 巡检配置主键
     * @return 巡检配置
     */
    @Override
    public SbInspectionConfig selectSbInspectionConfigByConfigId(Long configId)
    {
        return sbInspectionConfigMapper.selectSbInspectionConfigByConfigId(configId);
    }

    /**
     * 查询巡检配置列表
     * 
     * @param sbInspectionConfig 巡检配置
     * @return 巡检配置
     */
    @Override
    public List<SbInspectionConfig> selectSbInspectionConfigList(SbInspectionConfig sbInspectionConfig)
    {
        return sbInspectionConfigMapper.selectSbInspectionConfigList(sbInspectionConfig);
    }

    /**
     * 查询巡检配置列表(包含设备类型名称)
     * 
     * @param sbInspectionConfig 巡检配置
     * @return 巡检配置集合
     */
    @Override
    public List<SbInspectionConfig> selectSbInspectionConfigWithTypeList(SbInspectionConfig sbInspectionConfig)
    {
        return sbInspectionConfigMapper.selectSbInspectionConfigWithTypeList(sbInspectionConfig);
    }

    /**
     * 新增巡检配置
     * 
     * @param sbInspectionConfig 巡检配置
     * @return 结果
     */
    @Override
    public int insertSbInspectionConfig(SbInspectionConfig sbInspectionConfig)
    {
        // 校验配置编码唯一性
        if (StringUtils.isNotEmpty(sbInspectionConfig.getConfigCode()) && 
            !checkConfigCodeUnique(sbInspectionConfig))
        {
            throw new ServiceException("新增巡检配置'" + sbInspectionConfig.getConfigName() + "'失败，配置编码已存在");
        }
        
        // 如果配置编码为空，自动生成
        if (StringUtils.isEmpty(sbInspectionConfig.getConfigCode()))
        {
            sbInspectionConfig.setConfigCode(generateConfigCode());
        }
        
        sbInspectionConfig.setCreateTime(DateUtils.getNowDate());
        sbInspectionConfig.setCreateBy(SecurityUtils.getUsername());
        sbInspectionConfig.setIsDelete("0");
        return sbInspectionConfigMapper.insertSbInspectionConfig(sbInspectionConfig);
    }

    /**
     * 修改巡检配置
     * 
     * @param sbInspectionConfig 巡检配置
     * @return 结果
     */
    @Override
    public int updateSbInspectionConfig(SbInspectionConfig sbInspectionConfig)
    {
        // 校验配置编码唯一性
        if (StringUtils.isNotEmpty(sbInspectionConfig.getConfigCode()) && 
            !checkConfigCodeUnique(sbInspectionConfig))
        {
            throw new ServiceException("修改巡检配置'" + sbInspectionConfig.getConfigName() + "'失败，配置编码已存在");
        }
        
        sbInspectionConfig.setUpdateTime(DateUtils.getNowDate());
        sbInspectionConfig.setUpdateBy(SecurityUtils.getUsername());
        return sbInspectionConfigMapper.updateSbInspectionConfig(sbInspectionConfig);
    }

    /**
     * 批量删除巡检配置
     * 
     * @param configIds 需要删除的巡检配置主键
     * @return 结果
     */
    @Override
    public int deleteSbInspectionConfigByConfigIds(Long[] configIds)
    {
        return sbInspectionConfigMapper.deleteSbInspectionConfigByConfigIds(configIds);
    }

    /**
     * 删除巡检配置信息
     * 
     * @param configId 巡检配置主键
     * @return 结果
     */
    @Override
    public int deleteSbInspectionConfigByConfigId(Long configId)
    {
        return sbInspectionConfigMapper.deleteSbInspectionConfigByConfigId(configId);
    }

    /**
     * 校验配置编码是否唯一
     * 
     * @param sbInspectionConfig 巡检配置信息
     * @return 结果 (true=唯一,false=不唯一)
     */
    @Override
    public boolean checkConfigCodeUnique(SbInspectionConfig sbInspectionConfig)
    {
        Long configId = StringUtils.isNull(sbInspectionConfig.getConfigId()) ? -1L : sbInspectionConfig.getConfigId();
        SbInspectionConfig info = sbInspectionConfigMapper.selectSbInspectionConfigByConfigCode(sbInspectionConfig.getConfigCode());
        if (StringUtils.isNotNull(info) && info.getConfigId().longValue() != configId.longValue())
        {
            return false;
        }
        return true;
    }

    /**
     * 根据配置编码查询巡检配置
     * 
     * @param configCode 配置编码
     * @return 巡检配置
     */
    @Override
    public SbInspectionConfig selectSbInspectionConfigByConfigCode(String configCode)
    {
        return sbInspectionConfigMapper.selectSbInspectionConfigByConfigCode(configCode);
    }

    /**
     * 根据设备类型ID查询巡检配置列表
     * 
     * @param equipmentTypeId 设备类型ID
     * @return 巡检配置集合
     */
    @Override
    public List<SbInspectionConfig> selectSbInspectionConfigByEquipmentTypeId(Long equipmentTypeId)
    {
        return sbInspectionConfigMapper.selectSbInspectionConfigByEquipmentTypeId(equipmentTypeId);
    }

    /**
     * 导入巡检配置数据
     * 
     * @param configList 巡检配置列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importInspectionConfig(List<SbInspectionConfig> configList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(configList) || configList.size() == 0)
        {
            throw new ServiceException("导入巡检配置数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SbInspectionConfig config : configList)
        {
            try
            {
                // 验证是否存在这个巡检配置
                SbInspectionConfig existConfig = sbInspectionConfigMapper.selectSbInspectionConfigByConfigCode(config.getConfigCode());
                if (StringUtils.isNull(existConfig))
                {
                    config.setCreateBy(operName);
                    config.setCreateTime(DateUtils.getNowDate());
                    config.setIsDelete("0");
                    this.insertSbInspectionConfig(config);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、配置 " + config.getConfigName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    config.setConfigId(existConfig.getConfigId());
                    config.setUpdateBy(operName);
                    config.setUpdateTime(DateUtils.getNowDate());
                    this.updateSbInspectionConfig(config);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、配置 " + config.getConfigName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、配置 " + config.getConfigName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、配置 " + config.getConfigName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 生成配置编码
     * 
     * @return 配置编码
     */
    @Override
    public String generateConfigCode()
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateStr = sdf.format(new Date());
        
        // 查询当天已有的配置数量
        SbInspectionConfig query = new SbInspectionConfig();
        List<SbInspectionConfig> configs = sbInspectionConfigMapper.selectSbInspectionConfigList(query);
        
        int count = configs.size() + 1;
        String countStr = String.format("%03d", count);
        
        return "XJPZ" + dateStr + countStr;
    }
} 